package com.baseframe.blog.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baseframe.blog.entity.SnsBlog;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baseframe.blog.entity.request.SnsBlogRequest;
import com.baseframe.blog.entity.vo.SnsBlogVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**

* @description 针对表【sns_blog(博客表)】的数据库操作Mapper
* @createDate 2023-06-23 17:55:58
* @Entity com.baseframe.blog.entity.SnsBlog
*/
public interface SnsBlogMapper extends BaseMapper<SnsBlog> {

    @Select(value = "<script>" +
            "select sb.*,bu.username as author_name from sns_blog as sb left join  base_user as bu on sb.author = bu.id" +
            "<where>" +
            "sb.is_deleted = 0" +
            "<if test=\" request.title!=null &amp;&amp; request.title !='' \">" +
            "and sb.title like concat('%',#{request.title},'%')" +
            "</if>" +
            "</where>" +
            "order by sb.create_time" +
            "</script>")
    @Results(
            @Result(column = "author_name",property = "authorName",javaType = String.class)
    )
    Page<SnsBlogVO> blogPageList(Page<SnsBlogVO> page, @Param("request") SnsBlogRequest request);


    @Select("select sb.*,bu.username as author_name,(select count(*) from sns_blog_comment where blog_id = sb.id) as comment_count,(select count(*) from sns_blog_admire where blog_id = sb.id) as admire_count from sns_blog as sb left join base_user as bu on sb.author = bu.id where sb.is_deleted = 0 and sb.status = 1 and sb.authority = 1 order by sb.create_time desc  limit 8")
    List<SnsBlogVO> getNewList();

    @Select("<script>" +
            "select sb.*,bu.username as authorName,(select count(*) from sns_blog_comment where blog_id = sb.id) as comment_count,(select count(*) from sns_blog_admire where blog_id = sb.id) as admire_count  from sns_blog as sb left join base_user as bu on sb.author = bu.id" +
            "<where>" +
            " sb.is_deleted = 0 and sb.status = 1 and sb.authority = 1 " +
            "<if test=\" request.title!=null &amp;&amp; request.title !='' \">" +
            "and sb.title like concat('%',#{request.title},'%')" +
            "</if>" +
            "</where>" +
            "order by create_time desc" +
            "</script>")
    Page<SnsBlogVO> blogPageListByFront(Page<SnsBlogVO> page,@Param("request") SnsBlogRequest request);

    @Select("select sb.*,bu.username as author_name,(select count(*) from sns_blog_comment where blog_id = sb.id) as comment_count,(select count(*) from sns_blog_admire where blog_id = sb.id) as admire_count from sns_blog as sb left join base_user as bu on sb.author = bu.id where sb.is_deleted = 0 and sb.status = 1 and sb.authority = 1 and sb.id = #{request.id}")
    SnsBlogVO getDetails(@Param("request") SnsBlogRequest request);

    @Select("<script>" +
            "select sb.*,bu.username as author_name,(select count(*) from sns_blog_comment where blog_id = sb.id) as comment_count,(select count(*) from sns_blog_admire where blog_id = sb.id) as admire_count from sns_blog as sb left join base_user as bu on sb.author = bu.id" +
            "<where>" +
            "sb.is_deleted = 0 and sb.author = #{userId}" +
            "<if test=\" request.title!=null &amp;&amp; request.title !='' \">" +
            "and sb.title like concat('%',#{request.title},'%')" +
            "</if>" +
            "</where>" +
            "order by create_time desc" +
            "</script>")
    Page<SnsBlogVO> ownBlogPageList(Page<SnsBlogVO> page,@Param("request") SnsBlogRequest request,@Param("userId") Long userId);

}




